package controller

import (
	robot "github.com/AnonymousMister/dingtalk-robot"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gitlab-dingtalk/server/model"
	"gitlab-dingtalk/server/services"
)

type GitlabController struct {
	Ctx iris.Context
}

func (c *GitlabController) PostBy(token string) mvc.Result {
	dingtalk := &robot.DingDing{
		Secret:      c.Ctx.GetHeader("X-Gitlab-Token"),
		AccessToken: token,
	}
	gitlab := &model.GitLab{}
	var err error
	if err = c.Ctx.ReadJSON(gitlab); err != nil {
		return mvc.Response{
			Code:   400,
			Object: err,
		}
	}
	gitlab.Dingding = dingtalk
	gitlab.Event = c.Ctx.GetHeader("X-Gitlab-Event")
	err = services.GitlabService.Forward(gitlab)
	if err != nil {
		return mvc.Response{
			Code: 400,
			Err:  err,
		}
	}
	return mvc.Response{
		Code:   200,
		Object: "ok",
	}
}
